package inits

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"youku/youku-srv/basic/config"
	"youku/youku-srv/modle"
)

var err error

func InitMysql() {
	config.Once.Do(func() {
		// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
		dsn := "root:123456@tcp(127.0.0.1:3306)/youku?charset=utf8mb4&parseTime=True&loc=Local"
		config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
		if err != nil {
			panic(err)
		}
		log.Println("mysql连接成功")
		config.DB.AutoMigrate(&modle.Video{}, &modle.VideoEp{}, &modle.VideoData{}, &modle.User{})
		sqlDB, _ := config.DB.DB()
		sqlDB.SetMaxIdleConns(10)
		sqlDB.SetMaxOpenConns(100)
		sqlDB.SetConnMaxLifetime(0)
	})
}
